package com.tools.cleanmaster.ui.card.factory;

import android.support.v4.util.SparseArrayCompat;

import com.tools.ad.EntranceType;
import com.tools.cleanmaster.ui.card.ACardItem;
import com.tools.cleanmaster.ui.card.CardType;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lianzhan on 15/10/29.
 */
public class FunctionCardFactory {

    private static final SparseArrayCompat<SparseArrayCompat<CardType>> entranceCardMapping;

    /**
     * 默认本地卡片顺序
     */
    private static final SparseArrayCompat<CardType> sHomeCardList;
    private static final SparseArrayCompat<CardType> sFunctionCardList;

    static {
        sHomeCardList = new SparseArrayCompat<>();
        sHomeCardList.put(0, CardType.AUTO_CLEAN);
        sHomeCardList.put(1, CardType.LOADING_AD);
        sHomeCardList.put(2, CardType.TRASH); // 首页特有，无法复制到其他页面中
        sHomeCardList.put(3, CardType.RATE);
        sHomeCardList.put(4, CardType.PHONE_MEDIA);
        sHomeCardList.put(5, CardType.SWIPE);
        sHomeCardList.put(6, CardType.CHARGING);

        sFunctionCardList = new SparseArrayCompat<>();
        sFunctionCardList.put(0, CardType.AUTO_CLEAN);
        sFunctionCardList.put(1, CardType.LOADING_AD);
        sFunctionCardList.put(2, CardType.RATE);
        sFunctionCardList.put(3, CardType.PHONE_MEDIA);
        sFunctionCardList.put(4, CardType.SWIPE);
        sFunctionCardList.put(5, CardType.CHARGING);

        entranceCardMapping = new SparseArrayCompat<>(2);
        entranceCardMapping.put(EntranceType.HOME_TRASH_COMPLETED.ordinal(), sHomeCardList);
        entranceCardMapping.put(EntranceType.LANDING.ordinal(), sFunctionCardList);

        entranceCardMapping.put(EntranceType.HOME_POWER_COMPLETED.ordinal(), sFunctionCardList);
        entranceCardMapping.put(EntranceType.HOME_BOOST_COMPLETED.ordinal(), sFunctionCardList);
        entranceCardMapping.put(EntranceType.HOME_CPU_COMPLETED.ordinal(), sFunctionCardList);
    }

    private static List<ACardItem> getCardList(EntranceType entranceType, SparseArrayCompat<CardType> cardTypes) {
        List<ACardItem> list = new ArrayList<>();
        int size = cardTypes.size();
        for (int i = 0; i < size; i++) {
            int key = cardTypes.keyAt(i);
            if (cardTypes.get(key) == null) {
                continue;
            }
            String name = cardTypes.get(key).key;
            ACardItem item = ACardItem.createCardItem(name, key, entranceType);
            if (item != null && item.needShow(entranceType)) {
                list.add(item);
            }
        }
        return list;
    }

    public List<ACardItem> produceCards(EntranceType entranceType) {
        return getCardList(entranceType, entranceCardMapping.get(entranceType.ordinal()));
    }
}
